<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }
        
        .login-header p {
            color: #666;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s ease;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
        }
        
        .login-type {
            display: flex;
            margin-bottom: 20px;
            border-radius: 5px;
            overflow: hidden;
            border: 2px solid #e1e5e9;
        }
        
        .login-type button {
            flex: 1;
            padding: 10px;
            border: none;
            background: #f8f9fa;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .login-type button.active {
            background: #667eea;
            color: white;
        }
        
        .links {
            text-align: center;
            margin-top: 20px;
        }
        
        .links a {
            color: #667eea;
            text-decoration: none;
            margin: 0 10px;
            font-size: 14px;
        }
        
        .links a:hover {
            text-decoration: underline;
        }
        
        .error-message {
            color: #dc3545;
            text-align: center;
            margin-top: 10px;
            font-size: 14px;
        }
        
        .success-message {
            color: #28a745;
            text-align: center;
            margin-top: 10px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>欢迎登录</h1>
            <p>请选择登录类型并输入您的凭据</p>
        </div>
        
        <div class="login-type">
            <button type="button" class="active" onclick="switchLoginType('user')">用户登录</button>
            <button type="button" onclick="switchLoginType('admin')">管理员登录</button>
        </div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <button type="submit" class="login-btn">登录</button>
        </form>
        
        <div class="links">
            <a href="register.html">注册新用户</a>
            <a href="index.html">返回首页</a>
        </div>
        
        <div id="message"></div>
    </div>

    <script>
        let currentLoginType = 'user';
        
        function switchLoginType(type) {
            currentLoginType = type;
            const buttons = document.querySelectorAll('.login-type button');
            buttons.forEach(btn => btn.classList.remove('active'));
            event.target.classList.add('active');
            
            const header = document.querySelector('.login-header h1');
            if (type === 'admin') {
                header.textContent = '管理员登录';
            } else {
                header.textContent = '用户登录';
            }
        }
        
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const messageDiv = document.getElementById('message');
            
            try {
                let response;
                if (currentLoginType === 'admin') {
                    response = await fetch('/api/admin/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
                    });
                } else {
                    response = await fetch('/api/user/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
                    });
                }
                
                if (response.ok) {
                    const user = await response.json();
                    messageDiv.innerHTML = '<div class="success-message">登录成功！正在跳转...</div>';
                    
                    // 存储用户信息到localStorage
                    if (currentLoginType === 'admin') {
                        localStorage.setItem('adminInfo', JSON.stringify(user));
                        localStorage.setItem('userType', 'admin');
                    } else {
                        localStorage.setItem('userInfo', JSON.stringify(user));
                        localStorage.setItem('userType', 'user');
                    }
                    
                    // 跳转到相应的页面
                    setTimeout(() => {
                        if (currentLoginType === 'admin') {
                            window.location.href = 'admin_index.html';
                        } else {
                            window.location.href = 'home.html';
                        }
                    }, 1000);
                } else {
                    messageDiv.innerHTML = '<div class="error-message">用户名或密码错误</div>';
                }
            } catch (error) {
                messageDiv.innerHTML = '<div class="error-message">登录失败，请稍后重试</div>';
                console.error('登录错误:', error);
            }
        });
    </script>
</body>
</html>
